<?php
/**
 * Created by PhpStorm.
 * User: I073349
 * Date: 1/12/2016
 * Time: 1:14 PM
 */

namespace App\Traits;


use App\Models\Role;

trait UserRolesAndPermissions
{
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }


    /**
     * 返回用户有权限访问的菜单列表
     * @return array
     */
    public function menus()
    {
        // fetch all roles
        $menus = [];
        $this->roles()->get()->each(function($role) use(&$menus){
            $role->menus()->get()->each(function($menu) use (&$menus){
                $menus[$menu->name] = $menu;
            });
        });

        ksort($menus,SORT_NATURAL);
        return $menus;
    }

    public function isAdmin()
    {
        return true;
    }
}